#!/usr/bin/env ruby

LKP_SRC = ENV['LKP_SRC'] || File.dirname(File.dirname(File.realpath($PROGRAM_NAME)))

require "#{LKP_SRC}/lib/string_ext"

while (line = $stdin.gets)
  line = line.resolve_invalid_bytes
  case line
  # queries performed:
  #     read:                            134956458
  #     write:                           0
  #     other:                           19279494
  #     total:                           154235952
  # transactions:                        9639747 (16065.20 per sec.)
  # queries:                             154235952 (257043.25 per sec.)
  when /read:\s+(\d+)/
    puts "CNT.read: #{$1}"
  when /write:\s+(\d+)/
    puts "CNT.write: #{$1}"
  when /other:\s+(\d+)/
    puts "CNT.other: #{$1}"
  when /transactions:\s+(\d+)\s+\(([\d.]+) per sec.\)/
    puts "CNT.transactions: #{$1}"
    puts "RATE.transactions/s: #{$2}"
  when /queries:\s+(\d+)\s+\(([\d.]+) per sec.\)/
    puts "CNT.queries: #{$1}"
    puts "RATE.queries/s: #{$2}"
  # Latency (ms):
  #     min:                                    0.90
  #     avg:                                    1.78
  #     max:                                   57.61
  #     95th percentile:                        2.35
  #     sum:                               239794.59
  when /max:\s+([\d.]+)/
    puts "LAT.latency_max_ms: #{$1}"
  when /min:\s+([\d.]+)/
    puts "LAT.latency_min_ms: #{$1}"
  when /avg:\s+([\d.]+)/
    puts "LAT.latency_avg_ms: #{$1}"
  when /95th percentile:\s+([\d.]+)/
    puts "LAT.latency_95th_ms: #{$1}"
  when /sum:\s+([\d.]+)/
    puts "LAT.latency_sum_ms: #{$1}"
  # Threads fairness:
  #  events (avg/stddev):           33716.7500/24.71
  #  execution time (avg/stddev):   59.9486/0.01
  when /events \(avg\/stddev\):\s+([\d.]+)\/([\d.]+)/
    stddev = $2.to_f * 100 / $1.to_f
    puts "JIT.thread_events_stddev%: #{stddev}"
  when /execution time \(avg\/stddev\):\s+([\d.]+)\/([\d.]+)/
    stddev = $2.to_f * 100 / $1.to_f
    puts "JIT.thread_time_stddev%: #{stddev}"
  end
end
